package hx.utiks;

import hx.entity.*;
import hx.service.CallService;
import hx.service.NetService;
import hx.service.SendService;

import java.io.*;
import java.util.*;

public class CardUtils implements Serializable {
    private Map<String,MobileCard> cards=new TreeMap<>();
    private Map<String, List<ConsumInfo>> consumInofs=new TreeMap<>();
    private List<ConsumInfo> list=new ArrayList<>();
    public CardUtils() {

    }
    public void addCard(String s, MobileCard m){
        cards.put(s, m);
    }
    public Map<String, List<ConsumInfo>> getConsumInofs() {
            try {
                FileInputStream savaConsum = new FileInputStream("E:\\mobile-service\\consumInofs.dat");
                ObjectInputStream objectInputStream = new ObjectInputStream(savaConsum);
                consumInofs = (Map<String, List<ConsumInfo>>) objectInputStream.readObject();
                objectInputStream.close();

            } catch (FileNotFoundException e) {

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return consumInofs;
    }
    public Map<String, MobileCard> getCards() {
        try {
            FileInputStream savaCards=new FileInputStream("E:\\mobile-service\\cards.dat");
            ObjectInputStream objectInputStream=new ObjectInputStream(savaCards);
            cards=(Map<String,MobileCard>)objectInputStream.readObject();
            objectInputStream.close();

        } catch (FileNotFoundException e) {
            System.out.println("你还没有注册，请重新注册");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return cards;
    }
    public void savaconsumInofs() {
        try {
            FileOutputStream  fileOutputStream = new FileOutputStream("E:\\mobile-service\\consumInofs.dat");
            ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(consumInofs);
            objectOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void savaCards( Map<String,MobileCard> cards) throws IOException {
        this.cards=getCards();
        FileOutputStream fileOutputStream=new FileOutputStream("E:\\mobile-service\\cards.dat");
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(cards);
        objectOutputStream.close();
    }
    public void savaCards() throws IOException {
        FileOutputStream fileOutputStream=new FileOutputStream("E:\\mobile-service\\cards.dat");
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(cards);
        objectOutputStream.close();
    }
    public void initScene(){

    }
    public boolean isExistCard(String number,String passWord){
        this.getCards();
        if (cards.get(number).getPassWord().equals(passWord)){
            return true;
        }
        return  false;

    }
    public void isExistCard(String number){

    }
    public void  createNumber(){

    }
    public  void getNewNumber(int count){

    }
    public  void delCard(String number){
        getCards();
        cards.remove(number);
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void showRemainDetail(String num){
        cards.get(num).getSerPackage().shouwInfo();

    }
    public  void showAmountDstail(String number){
        cards.get(number).getSerPackage().show();
    }
    public void addConsumInfo(String number,ConsumInfo info){
        list.add(info);
        consumInofs.put(number,list);

    }
    public  void useSoso(String number){
        Random random=new Random();
        try {
            for (int a=0;a<6;a++){
                getCards();
                int c=random.nextInt(6);
                switch (c){
                    case 1:
                        if (cards.get(number).getSerPackage() instanceof SuperPackage ) {

                            if (cards.get(number).getMoney()>0){System.out.println("和父母沟通12Min");
                                gjSC(number,"时间",12);
                            }else System.out.println("你已经欠费请充值");
                        }else if (cards.get(number).getSerPackage() instanceof Talkpackage){


                            if (cards.get(number).getMoney()>0){ System.out.println("和父母沟通12Min");
                                gjTC(number,"时间",12);
                            }else System.out.println("你已经欠费请充值");
                        }else a--;
                        break;
                    case 2:
                        if (cards.get(number).getSerPackage() instanceof SuperPackage ){


                            if (cards.get(number).getMoney()>0){System.out.println("填写问卷调查，花费流量20MB");
                                gjSC(number,"Mb",20);
                            }else System.out.println("你已经欠费请充值");
                        }else if (cards.get(number).getSerPackage() instanceof NetPackage ){


                            if (cards.get(number).getMoney()>0){ System.out.println("填写问卷调查，花费流量20MB");
                                gjNC(number,"Mb",20);
                            }else System.out.println("你已经欠费请充值");
                        }else a--;
                        break;
                    case 3:

                        if (cards.get(number).getSerPackage() instanceof SuperPackage ){


                            if (cards.get(number).getMoney()>0){ System.out.println("发送短信退订10086，和每日快报，3条");
                                gjSC(number,"条",3);
                            }else System.out.println("你已经欠费请充值");
                        }else if (cards.get(number).getSerPackage() instanceof Talkpackage ){


                            if (cards.get(number).getMoney()>0){System.out.println("发送短信退订10086，和每日快报，3条");
                                gjTC(number,"条",3);
                            }else System.out.println("你已经欠费请充值");
                        }else a--;
                        break;
                    case 4:

                        if (cards.get(number).getSerPackage() instanceof SuperPackage ){

                            if (cards.get(number).getMoney()>0){    System.out.println("和朋友经行qq聊天花费38mb");
                                gjSC(number,"Mb",38);
                            }else System.out.println("你已经欠费请充值");
                        }else if (cards.get(number).getSerPackage() instanceof NetPackage ){

                            if (cards.get(number).getMoney()>0){   System.out.println("和朋友经行qq聊天花费38mb");
                                gjNC(number,"Mb",38);
                            }else System.out.println("你已经欠费请充值");
                        }else a--;
                        break;
                    case 5:
                        if (cards.get(number).getSerPackage() instanceof SuperPackage ){

                            if (cards.get(number).getMoney()>0){System.out.println("电话订餐，10min");
                                gjSC(number,"时间",10);
                            }else System.out.println("你已经欠费请充值");
                        }else if (cards.get(number).getSerPackage() instanceof Talkpackage ){

                            if (cards.get(number).getMoney()>0){   System.out.println("电话订餐，10min");
                                gjTC(number,"时间",10);
                            }else System.out.println("你已经欠费请充值");

                        }else a--;
                        break;
                    case 0:
                        if (cards.get(number).getSerPackage() instanceof SuperPackage ){

                            if (cards.get(number).getMoney()>0){ System.out.println("通知朋友更换微信号，30mb");
                                gjSC(number,"Mb",30);
                            }else System.out.println("你已经欠费请充值");

                        }else if (cards.get(number).getSerPackage() instanceof NetPackage ){

                            if (cards.get(number).getMoney()>0){  System.out.println("通知朋友更换微信号，30mb");
                                gjNC(number,"Mb",30);
                            }else System.out.println("你已经欠费请充值");

                        }else a--;
                        break;
                        default:
                            System.out.println(c);
                            break;

                }
            }


        }catch (NullPointerException e){
            System.out.println("你还没有注册");
        }

    }
    private void gjSC(String number,String type,int coo) {
        SuperPackage a=(SuperPackage)cards.get(number).getSerPackage();
        if (type.equals("时间"))a.call(coo,cards.get(number));
        if (type.equals("条"))a.send(coo,cards.get(number));
        if (type.equals("Mb"))a.netPlay(coo,cards.get(number));
        MobileCard aa=cards.get(number);
        aa.setSerPackage(a);
        cards.put(number,aa);
        addConsumInfo(number,new ConsumInfo(type,coo));
        savaconsumInofs();
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void gjTC(String number,String type,int coo) {
        Talkpackage a=(Talkpackage) cards.get(number).getSerPackage();
        if (type.equals("时间"))a.call(coo,cards.get(number));
        if (type.equals("条"))a.send(coo,cards.get(number));
        MobileCard aa=cards.get(number);
        aa.setSerPackage(a);
        cards.put(number,aa);
        addConsumInfo(number,new ConsumInfo(type,coo));
        savaconsumInofs();
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void gjNC(String number,String type,int coo) {
        NetPackage a=(NetPackage) cards.get(number).getSerPackage();
        if (type.equals("Mb"))a.netPlay(coo,cards.get(number));
        MobileCard aa=cards.get(number);
        aa.setSerPackage(a);
        cards.put(number,aa);
        addConsumInfo(number,new ConsumInfo(type,coo));
        savaconsumInofs();
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void showDescription(){
        File file=new File("E:\\mobile-service\\ddss.txt");
        try {
            FileInputStream ac=new FileInputStream(file);
          InputStreamReader acc=new InputStreamReader(ac,"GBK");
          int cc;
          while ((cc=acc.read())!=-1){
              System.out.print((char) cc);
          }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void changingPack(String number,String packNum){
        ServicePackage a=cards.get(number).getSerPackage();
        MobileCard aa=cards.get(number);
        double aaa=0;
        switch (packNum){
            case "超人套餐":
                aaa=78.0;
                break;
            case "话痨套餐":
                aaa=50.0;
                break;
            case "网虫套餐":
                aaa=68.0;
                break;
        }
        if (aa.getMoney()>aaa) {
            if (packNum.equals("超人套餐")){
                aa.setSerPackage(new SuperPackage());
                aa.setMoney(aa.getMoney()-78.0);}
            aa.setcTname("超人套餐");
            if (packNum.equals("话痨套餐")){
                aa.setSerPackage(new SuperPackage());
                aa.setMoney(aa.getMoney()-50.0);
                aa.setcTname("话痨套餐");
            }

            if (packNum.equals("网虫套餐")){
                aa.setSerPackage(new SuperPackage());
                aa.setMoney(aa.getMoney()-68.0);
                aa.setcTname("话痨套餐");
            }
        }else {
            System.out.println("余额不足请充值后在使用");
        }

        cards.put(number,aa);
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void printConsumInfo(String number){
        try {
            if (new File("E:\\mobile-service\\consumInofs.dat").exists())getConsumInofs();
            consumInofs=getConsumInofs();
            OutputStreamWriter opw=new OutputStreamWriter(new FileOutputStream("E:\\mobile-service\\"+number+".txt"),"UTF-8");
            if (consumInofs.get(number)!=null){

            String data="*****"+number+"消费记录******"+"\r\n"+"序号:\t"+"类型:\t"+"数据(通话(条)/上网(Mb)/短信(条))"+"\r";
            int sx=1;
            for (ConsumInfo info:consumInofs.get(number)) {
                String data2="\n\r"+String.valueOf(sx)+".\t"+info.getType()+"\t"+info.getConsumData()+"\n\r";
                data=data+data2;
                sx++;
            }
            opw.write(data);
            opw.close();
            }else System.out.println("您的消费记录为空");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (NullPointerException e){
            System.out.println("查无此人");
        }
    }
    public void chargeMoney(String number,double money){
     try {
         getCards();
         ServicePackage a=cards.get(number).getSerPackage();

        MobileCard aa=cards.get(number);
        aa.setSerPackage(new SuperPackage());
        aa.setMoney(aa.getMoney()+money);
        System.out.println("当前花费余额："+aa.getMoney());
        cards.put(number,aa);}catch (NullPointerException e){
         System.out.println("你还没有注册的");

     }
        try {
            savaCards();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
